/**
 * @Title: ProsuctDao.java
 * @Package com.netease.content_sales.dao
 * @Description: TODO(用一句话描述该文件做什么)
 * @author netease hzwangfei2014
 * @date 2016年3月29日 下午4:53:08
 * @version V1.0
 */
package com.netease.content_sales.dao;

import java.util.List;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.netease.content_sales.mate.Product;

/**
 * @ClassName: ProsuctDao
 * @Description: TODO(商品操作)
 * @author netease hzwangfei2014
 * @date 2016年3月29日 下午4:53:08
 */
public interface ProductDao {
	/**
	 * @Title: getProductList
	 * @Description: TODO(商品列表)
	 * @param userId
	 * @return List<Product> 返回类型
	 * @throws
	 */
	@Select("SELECT content.id AS id, content.title AS title, icon as image, content.price as price, IF (personId = #{userId}, TRUE, FALSE) AS isBuy,IF (personId <> '', TRUE, FALSE) AS isSell FROM content LEFT JOIN trx ON trx.contentId = content.id where content.ifSell = 0")
	public List<Product> getProductList(Number userId);

	/**
	 * @Title: getProductDetail
	 * @Description: TODO(商品明细)
	 * @param id
	 * @param userId
	 * @return Product 返回类型
	 * @throws
	 */
	@Select("SELECT content.id AS id, content.title AS title, icon as image, content.price, trx.price as buyPrice, abstract as summary, text as detail , IF (personId = #{userId}, TRUE, FALSE) AS isBuy,IF (personId <> '', TRUE, FALSE) AS isSell FROM content LEFT JOIN trx ON trx.contentId = content.id where content.id = #{id} and content.ifSell = 0")
	public Product getProductDetail(@Param("id") Number id, @Param("userId") Number userId);

	/**
	 * @Title: updateProduct
	 * @Description: TODO(更新商品)
	 * @param product
	 * @return Integer 返回类型
	 * @throws
	 */
	@Update("update content set title=#{title},icon=#{image},price=#{price},text=#{detail},abstract=#{summary} where id= #{id}")
	public Integer updateProduct(Product product);

	@Insert("insert into content(title, icon, price, text, abstract) values(#{title},#{image},#{price},#{detail},#{summary})")
	@Options(useGeneratedKeys = true, keyProperty = "id") // 添加该行，content中的id将被自动添加
	public Integer insertProduct(Product product);

	@Update("update content set ifSell = 1 where id = #{id} ")
	public Integer deleteProduct(@Param("id") Number id, @Param("seller") Number seller);
}
